﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.BUS;
using DoAn.DTO;
using System.Data;
using DevExpress.Web.ASPxClasses;


namespace DoAn.BAN
{
    public partial class BanTrang4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable tb=new DataTable();
                tb=HinhThucThanhToanBUS.loadHinhThuc();
                GridView1.DataSource = tb;
                GridView1.DataBind();
                LoadVung();
                LoadDanhSachPhi();                      
                
            }
            
        }

   
        public void LoadVung()
        {
            //Hàm load lên dropdowlist
            cmbVung.DataSource = VungMienBUS.loadVung();
            cmbVung.DataTextField = "TenVung";
            cmbVung.DataValueField = "MaVung";
            cmbVung.DataBind();
        }
        public void LoadDanhSachPhi()
        {
            DanhSachPhiTheoVungDTO ds = new DanhSachPhiTheoVungDTO();
            ds.MaVung = int.Parse((cmbVung.SelectedValue).ToString());
            DataTable tb = new DataTable();
            tb = DanhSachPhiTheoVungBUS.Loadphi(ds);
            GridView2.DataSource =tb ;
            GridView2.DataBind();
        }
        protected void cmbVung_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDanhSachPhi();
        }
        public void LayHinhThucThanhToan()
        {
            int dem = 0;
           
            List<HinhThucThanhToanDTO> htl = new List<HinhThucThanhToanDTO>();
            foreach (GridViewRow GVR in GridView1.Rows)
            {
                CheckBox check = (CheckBox)GVR.Cells[0].FindControl("CheckBox1");
                if (check.Checked == true)
                {
                    // lay ma
                    HinhThucThanhToanDTO ht = new HinhThucThanhToanDTO();
                    ht.MaHinhThucTT = Convert.ToInt32(GridView1.DataKeys[dem].Value.ToString());
                    ht.TenHinhThucTT = GridView1.Rows[dem].Cells[1].Text;
                    //Response.Write(ht.TenHinhThucTT);
                    //Response.Write(ht.MaHinhThucTT);

                    htl.Add(ht);

                }
                dem++;
                
            }
            Session["HinhThucTT"] = htl;
        }

        public void LayChiPhiVanChuyen()
        {
            int dem = 0;
            ChiPhiVanChuyenDTO vc = new ChiPhiVanChuyenDTO();
            List<ChiPhiVanChuyenDTO> lvc = new List<ChiPhiVanChuyenDTO>();

            //List<ChiPhiVanChuyenDTO> lvc = (List<ChiPhiVanChuyenDTO>)Session["phivanchuyen"]
            
            foreach (GridViewRow GVR in GridView2.Rows)
            {
                CheckBox check = (CheckBox)GVR.Cells[0].FindControl("CheckBox2");
                if (check.Checked == true)
                {
                    //lay ma vung
                    vc.MaPhiVung = int.Parse(GridView2.DataKeys[dem].Value.ToString());
                    //phi
                    TextBox tb1 = (TextBox)GVR.Cells[2].FindControl("TextBox1");
                    vc.CuocPhi = int.Parse(tb1.Text);
                    //ghi chu
                    TextBox tb2 = (TextBox)GVR.Cells[3].FindControl("TextBox2");
                    vc.GhiChu = tb2.Text;
                    lvc.Add(vc);
                }
                dem++;
                
            }
            Session["PhiVanChuyen"] = lvc;
        }

        protected void bntNext_Click(object sender, EventArgs e)
        {
            LayChiPhiVanChuyen();
            LayHinhThucThanhToan();
            SanDauGiaDTO san = new SanDauGiaDTO();
            san = (SanDauGiaDTO)HttpContext.Current.Session["SanPham"];
            if (ckbTraHang.CheckState == CheckState.Checked)
            {
                san.HoTroTraSP = true;
                san.ThoiGianTraSP = int.Parse(speTHTraHang.Value.ToString()) ;
                san.HinhThucHoanLaiTien = txtHinhThucHoanTien.Text;
            }
            else
            {
                san.HoTroTraSP = false;
                san.ThoiGianTraSP = 0;
                san.HinhThucHoanLaiTien = "";
            }
            san.DiaChiShip = txtDiaDiem.Text;
            Session["SanPham"] = san;
            VungMienDTO vm = new VungMienDTO();
            vm.MaVung = int.Parse(cmbVung.SelectedValue);
            Session["VungMien"] = vm;
            Response.Redirect("~/BAN/BanTrang5.aspx");
        }
       
    }
}